// 防抖
export const shakeProof = (fn, wait) => {
	var timer = null
	return function(...args) {
		if (timer !== null) {
			clearTimeout(timer)
		}
		let that = this
		timer = setTimeout(() => {
			fn(...args, that)
			timer = null
		}, wait)
	}
}
// 节流
export const throttle = (fn, wait) => {
	var timer = null
	return function() {
		var context = this
		var args = arguments
		if (!timer) {
			timer = setTimeout(function() {
				fn.apply(context, args)
				timer = null
			}, wait)
		}
	}
}

export const formatTime = (time) => {
	if (typeof time !== 'number' || time < 0) {
		return time
	}

	var hour = parseInt(time / 3600)
	time = time % 3600
	var minute = parseInt(time / 60)
	time = time % 60
	var second = time

	return ([hour, minute, second]).map(function(n) {
		n = n.toString()
		return n[1] ? n : '0' + n
	}).join(':')
}

export const formatLocation = (longitude, latitude) => {
	if (typeof longitude === 'string' && typeof latitude === 'string') {
		longitude = parseFloat(longitude)
		latitude = parseFloat(latitude)
	}

	longitude = longitude.toFixed(2)
	latitude = latitude.toFixed(2)

	return {
		longitude: longitude.toString().split('.'),
		latitude: latitude.toString().split('.')
	}
}
// 倒计时
export const timeDown = (timeNum, fn) => {
	var s = timeNum
	var timer = setInterval(function() {
		s--
		if (s === 0) {
			clearInterval(timer)
			s = timeNum
		}
		return fn(s)
	}, 1000)
}
// 计算倒计时 按照天，时，分，秒
export const getCountDown = (endTime) => {
	// endTime = 1614155555
	const nowDate = new Date()
	const nowTime = parseInt(nowDate.getTime() / 1000) // 当前时间戳改为10位,因为后台返回的时间戳是10位
	const t = endTime - nowTime
	if (t <= 0) {
		return false
	}
	let day = Math.floor(t / 60 / 60 / 24)
	let hour = Math.floor(t / 60 / 60 % 24)
	let minute = Math.floor(t / 60 % 60)
	let second = Math.floor(t % 60)
	// if (day < 10) {
	//   day = '0' + day TODO: padStart是字符串方法
	// }
	day = String(day).padStart(2, '0')
	hour = String(hour).padStart(2, '0')
	minute = String(minute).padStart(2, '0')
	second = String(second).padStart(2, '0')
	// if (hour < 10) {
	//   // hour = '0' + hour
	// }
	// if (minute < 10) {
	//   // minute = '0' + minute
	// }
	// if (second < 10) {
	//   // second = '0' + second
	// }
	return {
		day,
		hour,
		minute,
		second
	}
}

// 时间

export const remainTime = (timeSpacing) => {
	let day = 0
	let hour = 0
	let minute = 0
	let second = 0
	// let second = 0
	day = Math.floor(timeSpacing / (24 * 60 * 60 * 1000))
	hour = Math.floor(timeSpacing % (24 * 60 * 60 * 1000) / (60 * 60 * 1000))
	minute = Math.floor((timeSpacing - day * 24 * 60 * 60 * 1000) % (60 * 60 * 1000) / (60 * 1000))
	second = Math.floor((timeSpacing - day * 24 * 60 * 60 * 1000 - hour * 60 * 60 * 1000) % (60 * 1000) / 1000)
	return ` ${day > 0 ? day + '天' : ''}  ${hour}  小时  ${minute}  分钟  ${second}  秒`
}


// 价格截取整数
export const cuntInit = (price) => {
	const index = String(price).indexOf('.')
	const init = String(price).substring(0, index)
	return init
}
// 价格截取小数
export const countDeci = (price) => {
	const index = String(price).indexOf('.')
	const deci = String(price).substring(index)
	return deci
}

// 解析分享scene参数
export const getKey = function(str, key) {
	const reg = new RegExp('(^|&)' + key + '=([^&]*)(&|$)')
	const matchStr = str.match(reg)
	return matchStr ? decodeURI(matchStr[2]) : ''
}


export function moreBarChartData(data) {
	let chartData = {
		categories: [],
		series: [{
			name: '',
			data: []
		}],
		setObj: {}
	}

	for (let i = 0; i < data.length; i++) {
		chartData.categories.push(data[i].name)
		chartData.series[0].data.push(Number(data[i].value))
	}
	chartData.setObj = setSplitNumber(chartData.series[0].data)
	console.log(chartData)
	return chartData

}
export function barChartData(data) {
	let chartData = {
		categories: [],
		series: [],
		setObj: {}
		// series: [{
		// 	data: []
		// }],
	}
	let yData = []
	let dataY = []
	let yValues = []
	for (let i = 0; i < data.length; i++) {

		chartData.categories.push(data[i].name)
		// chartData.series.push(data[i].values[b])

		let dataValue = 0
		for (let b = 0; b < data[i].values.length; b++) {
			const obj = {
				name: data[i].values[b].name,
				data: Number(data[i].values[b].value)
			}
			dataValue += Number(data[i].values[b].value)
			yData.push(obj)
		}
		yValues.push(dataValue)

	}
	console.log(yValues, 'yValues')
	let max = 1
	if (yValues.length != 0) {
		max = Math.max.apply(null, yValues);
	}

	for (let m = 0; m < yData.length; m++) {
		let index = chartData.series.findIndex(function(item) {
			return item.name === yData[m].name
		});
		if (index !== -1) {
			chartData.series[index].data.push(yData[m].data);
		} else {
			chartData.series.push({
				name: yData[m].name,
				data: [yData[m].data]
			})
		}
		dataY.push(yData[m].data)

	}
	chartData.setObj = setBarSplitNumber(dataY, max)

	return chartData


}
export function ringBarChartData(data) {
	let chartData = {
		series: [{
			data: []
		}],
	}
	let total = 0
	for (let j = 0; j < data.length; j++) {
		total += Number(data[j].value)

	}
	for (let i = 0; i < data.length; i++) {
		let lableValue = 0
		if (total > 0) {
			lableValue = (Number(data[i].value) / total * 100).toFixed(2)
		}
		let obj = {
			name: data[i].name,
			value: Number(data[i].value),
			labelText: data[i].name + ' ' + lableValue + '%'
		}
		chartData.series[0].data.push(obj)
	}
	console.log(chartData, 'ringBarChartData')
	return chartData

}
export function setBarSplitNumber(arr, maxVal) {
	let splitNumber = 5;
	// let sum = 0;
	let bigmax = 1;
	if (arr.length != 0) {
		let max = Math.max.apply(null, arr);
		let min = Math.min.apply(null, arr);
		if ((Number(max) - Number(min) >= 5) || (Number(max) - Number(min) == 0)) {
			splitNumber = 5;
		} else {
			splitNumber = Number(max) - Number(min);
		}
		bigmax = Number(max)
		// for (let i = 0; i < arr.length; i++) {
		// 	sum += Number(arr[i])
		// }
	}
	// Math.ceil 向上取整
	const setObj = {
		sum: Math.ceil(maxVal),
		max: Math.ceil(bigmax),
		splitNumber: Math.ceil(splitNumber)
	}
	console.log(setObj, 'setObj')
	return setObj;

}
export function setSplitNumber(arr) {
	let splitNumber = 5;
	let bigmax = 1
	if (arr.length != 0) {
		let max = Math.max.apply(null, arr);
		let min = Math.min.apply(null, arr);
		if ((Number(max) - Number(min) >= 5) || (Number(max) - Number(min) == 0)) {
			splitNumber = 5;
		} else {
			splitNumber = Number(max) - Number(min);
		}
		bigmax = Number(max)
	}
	const setObj = {
		max: bigmax,
		splitNumber: splitNumber
	}
	console.log(setObj, 'setObj')
	return setObj;

}
export function twoBarChartData(data) {
	let chartData = {
		categories: [],
		series: [],
		setObj: {}
	}
	let yData = [],
		dataY = []
	for (let i = 0; i < data.length; i++) {

		chartData.categories.push(data[i].name)
		for (let b = 0; b < data[i].values.length; b++) {
			const obj = {
				name: data[i].values[b].name,
				data: Number(data[i].values[b].value)
			}
			yData.push(obj)
			dataY.push(Number(data[i].values[b].value))
		}

	}
	chartData.setObj = setSplitNumber(dataY)
	console.log(yData, 'yData')

	for (let m = 0; m < yData.length; m++) {
		let index = chartData.series.findIndex(function(item) {
			return item.name === yData[m].name
		});
		if (index !== -1) {
			chartData.series[index].data.push(yData[m].data);
		} else {
			chartData.series.push({
				name: yData[m].name,
				data: [yData[m].data]
			})
		}
	}
	// chartData.series = [...data]
	console.log(chartData, 'twoBarChartData')
	return chartData
}

export function pieChartData(data) {
	let chartData = {
		series: [{
			data: []
		}],
	}
	for (let b = 0; b < data.length; b++) {
		const obj = {
			name: data[b].name,
			value: Number(data[b].value)
		}
		chartData.series[0].data.push(obj)
	}
	console.log(JSON.stringify(chartData), 'pieChartData')
	return chartData
}
export function xBarChartData(data) {
	let chartData = {
		categories: [],
		series: [],
		setObj: {}
	}
	let yData = []
	for (let i = 0; i < data.length; i++) {
		chartData.categories.push(data[i].name)
		const obj = {
			name: data[i].name,
			value: [Number(data[i].value)]
		}
		yData.push(Number(data[i].value))
	}
	chartData.setObj = setSplitNumber(yData)
	chartData.series.push({
		name: '',
		data: yData
	})
	console.log(chartData, 'xBarChartData')
	return chartData
}

export function lineChartData(data) {
	let chartData = {
		categories: [],
		series: []
		// series: [{
		// 	data: []
		// }],
	}
	let yData = []
	for (let i = 0; i < data.length; i++) {

		chartData.categories.push(data[i].name)
		// chartData.series.push(data[i].values[b])

		for (let b = 0; b < data[i].values.length; b++) {
			const obj = {
				name: data[i].values[b].name,
				data: Number(data[i].values[b].value)
			}
			yData.push(obj)
		}

	}
	console.log(yData, 'yData')

	for (let m = 0; m < yData.length; m++) {
		let index = chartData.series.findIndex(function(item) {
			return item.name === yData[m].name
		});
		if (index !== -1) {
			chartData.series[index].data.push(yData[m].data);
		} else {
			chartData.series.push({
				name: yData[m].name,
				data: [yData[m].data]
			})
		}
	}
	console.log(chartData, 'chartDatachartData')
	return chartData
	// let chartData = {
	// 	categories: [],
	// 	series: [],
	// }

	// for (let i = 0; i < data.length; i++) {
	// 	chartData.categories.push(data[i].name)
	// 	for (let j = 0; j < data[i].values.length; j++) {
	// 		let obj = {
	// 			name: data[0].values[j].name,
	// 			data: data[i].values[j].value
	// 		}
	// 		chartData.series.push(obj)
	// 	}
	// }
	// console.log(chartData)
	// return chartData
}
export function radarChartData(data) {
	let chartData = {
		categories: [],
		series: [{
			name: '物资领用金额分布',
			data: []
		}]
	}

	for (let i = 0; i < data.length; i++) {
		chartData.categories.push(data[i].name)
		chartData.series[0].data.push(Number(data[i].value))
		// let arr2 = new Array(data.length).fill(Number(data[i].value))
		// let arr2 = new Array(data.length).fill(0)
		// arr2[0] = Number(data[i].value)
		// const obj = {
		// 	name: data[i].name,
		// 	data: arr2
		// }
		// chartData.series.push(obj)
	}
	if(data.length===1){
		chartData.categories.push('')
		chartData.series[0].data.push('')
	}
	console.log(chartData, 'radarChartData')
	return chartData

}

/**
 * 数字不够x位，前面补0
 * @param {*} num 数字
 * @param {*} len 补零长度
 * @returns  0001
 */
export function formatZero(num, len) {
	let numberString = ''
	if (String(num).length > len) {
		numberString = num;
	}
	numberString = (Array(len).join(0) + num).slice(-len)
	return numberString

}
